package io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Demo5 {

	public static void main(String[] args) {
		read("F:/pic/11.txt");
	}
	
	//public static void copy()
	
	public static void write(String path,String str){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		Writer writer = null;
		try {
			writer = new FileWriter(file);
			writer.write(str);
			writer.append("dfjdb");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != writer){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void read(String path){
		File file = new File(path);
		if(!file.exists() || file.isDirectory()){
			System.out.println("文件读取失败");
		}
		//选择流
		Reader reader = null;
		try {
			reader = new FileReader(file);
			char[] buffer = new char[10];
			int len = 0;
			while(-1 != (len = reader.read(buffer))){
				System.out.println(new String(buffer,0,len));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != reader){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
